package copyengine.resource.lazyload
{
	import copyengine.resource.CEResConfig;
	import copyengine.resource.CEResManger;
	import copyengine.resource.dto.LazyLoadInitDto;
	import copyengine.resource.dto.ResObjectRequestParamDTO;
	import copyengine.utils.debug.CELog;
	import copyengine.utils.tick.GlobalTick;

	import flash.display.MovieClip;

	public class CELazyLoadMovieClipeContainer extends MovieClip implements ICELazyLoadContainer
	{
		private static const TYPE_DO_NOTHING:int = 0;
		private static const TYPE_GOTO_AND_STOP:int = 1;
		private static const TYPE_GOTO_AND_PLAY:int = 2;

		private var fileName:String;
		private var className:String;

		private var childMc:MovieClip;
		private var childStopFrame:Object;
		private var childInitType:int = TYPE_DO_NOTHING;

		private var loadComplateCallBackFunctionVector:Vector.<Function>;

		public function CELazyLoadMovieClipeContainer()
		{
		}

		//============================================//
		//==Initialize&Dispose  Function
		//============================================//
		public function initialize(_dto:LazyLoadInitDto):void
		{
			fileName = _dto.fileName;
			className = _dto.className;
			LazyLoadContainerInitHelper.addLazyLoadIcon(_dto, this);
		}

		//============================================//
		//==Interface  ICELazyLoadContainer Function
		//============================================//

		public function notificationLazyLoaderTargetLoadComplate():void
		{
			LazyLoadContainerInitHelper.removeLazyLoadIcon(this);

			var dto:ResObjectRequestParamDTO = new ResObjectRequestParamDTO();
			dto.fileType = CEResConfig.FILE_TYPE_SWF;
			dto.displayObjectType = CEResConfig.SWF_TYPE_MOVIECLIPE;
			dto.className = className;

			childMc = CEResManger.instance.getResFile(fileName).getObject(dto) as MovieClip;
			addChild(childMc);

			switch (childInitType)
			{
				case TYPE_DO_NOTHING:
					break;
				case TYPE_GOTO_AND_PLAY:
					childMc.gotoAndPlay(childStopFrame);
					break;
				case TYPE_GOTO_AND_STOP:
					childMc.gotoAndStop(childStopFrame);
					break;
			}
			for each (var f:Function in loadComplateCallBackFunctionVector)
			{
				f && f();
			}
		}

		public function addLoadComplateCallBackFunction(_f:Function):void
		{
			if (childMc == null)
			{
				if (loadComplateCallBackFunctionVector == null)
				{
					loadComplateCallBackFunctionVector = new Vector.<Function>();
				}
				loadComplateCallBackFunctionVector.push(_f);
			}
			else
			{
				_f();
			}
		}

		public function removeLoadComplateCallBack(_f:Function):void
		{
			if (loadComplateCallBackFunctionVector != null)
			{
				for (var i:int = 0; i < loadComplateCallBackFunctionVector.length; i++)
				{
					if (loadComplateCallBackFunctionVector[i] == _f)
					{
						loadComplateCallBackFunctionVector.splice(i, 1);
						break;
					}
				}
			}
		}


		//============================================//
		//==Override MoveClip Function
		//============================================//

		override public function gotoAndPlay(frame:Object, scene:String = null):void
		{
			if (childMc == null)
			{
				childStopFrame = frame;
				childInitType = TYPE_GOTO_AND_PLAY;
			}
			else
			{
				childMc.gotoAndPlay(frame);
			}
		}

		override public function gotoAndStop(frame:Object, scene:String = null):void
		{
			if (childMc == null)
			{
				childStopFrame = frame;
				childInitType = TYPE_GOTO_AND_STOP;
			}
			else
			{
				childMc.gotoAndStop(frame);
			}
		}

		override public function get totalFrames():int
		{
			if (childMc)
			{
				return childMc.totalFrames;
			}
			else
			{
				CELog.err("CELazyLoadMovieClipeContainer->getTotalFrame: CELazyLoadMovieClipeContainer not loadComplate");
				return 1;
			}
		}


	}
}
